package cn.darkhorse.workorder.service.impl;

import java.util.List;

import cn.darkhorse.workorder.domain.QmsWorkorderBalanceInfo;
import cn.darkhorse.workorder.service.IQmsWorkorderBalanceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.workorder.mapper.QmsWorkorderSampleDeliveryMapper;
import cn.darkhorse.workorder.domain.QmsWorkorderSampleDelivery;
import cn.darkhorse.workorder.service.IQmsWorkorderSampleDeliveryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 取送样品交接Service业务层处理
 *
 * @author ytq
 * @date 2024-07-19
 */
@Service
public class QmsWorkorderSampleDeliveryServiceImpl extends ServiceImpl<QmsWorkorderSampleDeliveryMapper, QmsWorkorderSampleDelivery> implements IQmsWorkorderSampleDeliveryService
{

    @Autowired
    private IQmsWorkorderBalanceInfoService workorderBalanceInfoService;



    @Override
    public QmsWorkorderSampleDelivery getByOrderNumber(String orderNumber) {
       return lambdaQuery().eq(QmsWorkorderSampleDelivery::getOrderNumber, orderNumber).one();
    }



    /**
     * 新增取送样品交接
     *
     * @param qmsWorkorderSampleDelivery 取送样品交接
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveQmsWorkorderSampleDelivery(QmsWorkorderSampleDelivery qmsWorkorderSampleDelivery) {
        QmsWorkorderBalanceInfo balanceInfoDelivery = workorderBalanceInfoService.lambdaQuery().eq(QmsWorkorderBalanceInfo::getOrderNumber, qmsWorkorderSampleDelivery.getOrderNumber()).eq(QmsWorkorderBalanceInfo::getType, 1).one();
        if (balanceInfoDelivery==null){
            balanceInfoDelivery = new QmsWorkorderBalanceInfo();
        }
        balanceInfoDelivery.setOrderId(qmsWorkorderSampleDelivery.getId());
        balanceInfoDelivery.setOrderNumber(qmsWorkorderSampleDelivery.getOrderNumber());
        balanceInfoDelivery.setType(1);
        balanceInfoDelivery.setTestRecord(qmsWorkorderSampleDelivery.getDeliveryTestRecord());
        balanceInfoDelivery.setTesterCode(qmsWorkorderSampleDelivery.getDeliveryTesterCode());
        balanceInfoDelivery.setTesterModel(qmsWorkorderSampleDelivery.getDeliveryTesterModel());
        balanceInfoDelivery.setRemark(qmsWorkorderSampleDelivery.getDeliveryRemark());
        balanceInfoDelivery.setTestTime(qmsWorkorderSampleDelivery.getDeliveryTestTime());

        workorderBalanceInfoService.saveOrUpdate(balanceInfoDelivery);
        System.out.println("saveQmsWorkorderSampleDelivery"+qmsWorkorderSampleDelivery.toString());
        return saveOrUpdate(qmsWorkorderSampleDelivery);
    }



}
